package com.sanyu.lovefishing.ui;

import android.util.Log;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.gyf.immersionbar.ImmersionBar;
import com.lyf.core.ui.activity.BaseActivity;
import com.sanyu.lovefishing.R;
import com.sanyu.lovefishing.databinding.ActivityMainBinding;
import com.sanyu.lovefishing.ui.fragment.IndexFragment;
import com.sanyu.lovefishing.ui.fragment.MapFragment;
import com.sanyu.lovefishing.ui.fragment.MoreFragment;
import com.sanyu.lovefishing.ui.fragment.WeatherFragment;
import com.sanyu.lovefishing.ui.widgets.bottom_bar.BottomBar;
import com.sanyu.lovefishing.ui.widgets.bottom_bar.BottomBarTab;

/**
 * 首页
 */
public class MainActivity extends BaseActivity<ActivityMainBinding> {

    private static final String TAG = "MainActivity";


    //天气
    private WeatherFragment mWeatherFragment=WeatherFragment.newInstance();
    //指数
    private IndexFragment mIndexFragment=IndexFragment.newInstance();
    //地图
    private MapFragment mMapFragment=MapFragment.newInstance();
    //更多
    private MoreFragment mMoreFragment= MoreFragment.newInstance();

    private Fragment mCurrFragment;

    @Override
    protected ActivityMainBinding getViewBinding() {
        return ActivityMainBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initStatusBar() {
        ImmersionBar.with(this)
                .statusBarDarkFont(false)
                .statusBarColor(R.color.color_theme_color)
                .init();
    }

    @Override
    protected void init() {
        super.init();
        mCurrFragment=mIndexFragment;
        mViewBinding.bottomBar.addItem(new BottomBarTab(this,R.mipmap.tab_forecast_icon,R.mipmap.tab_forecast_icon,"天气"));
        mViewBinding.bottomBar.addItem(new BottomBarTab(this,R.mipmap.tab_feed_icon,R.mipmap.tab_feed_icon,"指数"));
        mViewBinding.bottomBar.addItem(new BottomBarTab(this,R.mipmap.tab_maps_icon,R.mipmap.tab_maps_icon,"地图"));
        mViewBinding.bottomBar.addItem(new BottomBarTab(this,R.mipmap.tab_more_icon,R.mipmap.tab_more_icon,"更多"));
        mViewBinding.bottomBar.setOnTabSelectedListener(new BottomBar.SimpleTabSelectedListener(){
            @Override
            public boolean onTabSelected(int position, int prePosition) {
                Log.e(TAG, "----------------------onTabSelected  position: "+position);
                switch (position){
                    case 0:
                        switchContent(mWeatherFragment);
                        break;
                    case 1:
                        switchContent(mIndexFragment);
                        break;
                    case 2:
                        switchContent(mMapFragment);
                        break;
                    case 3:
                        switchContent(mMoreFragment);
                        break;
                }
                return true;
            }
        });
        mViewBinding.bottomBar.setCurrentItem(1);
        switchContent(mIndexFragment);
    }

    public void switchContent(Fragment to) {
        if (mCurrFragment == null || to == null) {
            return;
        }
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (!to.isAdded()) {
            transaction.hide(mCurrFragment).add(R.id.flContainer, to, to.getClass().getSimpleName()).commitAllowingStateLoss();
        } else {
            transaction.hide(mCurrFragment).show(to).commitAllowingStateLoss();
        }
        mCurrFragment=to;
    }
}
